package com.huawei.aiservice.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.*;

@Slf4j
public class FileUtils {
    public static String getFileContent(String path) {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        try (InputStream inputStream = ClassLoader.getSystemClassLoader().getResourceAsStream("prompt/inconsistent.md");) {
            return getFileContent(inputStream);
        } catch (IOException e) {
            log.error("getFileContent error", e);
            return null;
        }
    }

    public static String getFileContent(InputStream inputStream) {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        try{
            if (inputStream == null) {
                return null;
            }
            int nRead;
            byte[] data = new byte[16384]; // 16KB buffer
            while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, nRead);
            }
            buffer.flush();
            return buffer.toString("UTF-8");
        } catch (IOException e) {
            log.error("getFileContent error", e);
            return null;
        }
    }
}
